<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button v-if="in_right('LogDividend/list_money')" @click="to_money_list(row)" size="small">分红金额记录</el-button>
      <el-button v-if="in_right('LogDividend/edit') && row.status==0" @click="to_edit(row)" size="small">编辑</el-button>
      <el-button v-if="in_right('LogDividend/admin_log')" @click="show_admin_log(row)" size="small">操作记录</el-button>
      <el-button v-if="in_right('LogDividend/edit_dividend') && row.status==0" @click="edit_dividend(row)" size="small">执行分红</el-button>
    </template>
    <template #dividend_result="{row}">
      <p v-for="(v,k) in row.dividend_result">{{ level_map[k] }}：{{ v.user_count }}人，每人{{ v.dividend_money_unit }}</p>
    </template>
  </list_url>

  <el-dialog v-model="admin_log.show" width="850px" type="0">
    <list :cols="admin_log.list_cols" :data="admin_log.list">
      <template #log_dividend="{row,col}">
        <json_param :config="change_form_config" :value="row[col.name]" is_info="1"></json_param>
      </template>
    </list>
  </el-dialog>
</template>
<script>
export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');

    if(in_right_admin('LogDividend/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }

    let status_map = this.admin_config('status_alias_map.log_dividend.status');
    let not_dividend_action_map = this.admin_config('status_alias_map.log_dividend.not_dividend_action');
    let can_dividend_map = this.admin_config('status_alias_map.log_dividend.can_dividend');

    let change_form_config = {
      dividend_ratio:{name:'dividend_ratio', label:'分红比例', type:'text_one', config:'number', style:{margin:'2px 0'}},
      dividend_level:{name:'dividend_level', label:'分红等级', type:'checkbox', config:[], style:{margin:'2px 0'}},
      dividend_config:{name:'dividend_config', label:'分红配比', type:'json_param_checkbox', config:[], style:{margin:'2px 0'}},
      pay_money_need:{name:'pay_money_need', label:'业绩需求', type:'text_one', config:'number', style:{margin:'2px 0'}},
      can_dividend:{name:'can_dividend', label:'分红模式', type:'radio', config:can_dividend_map, style:{margin:'2px 0'}},
      not_dividend_action:{name:'not_dividend_action', label:'未分红的处理', type:'radio', config:not_dividend_action_map, style:{margin:'2px 0'}},
    }

    let forms_config = {
      pay_money_need: {label:'业绩需求', name:'where[pay_money_need][between]',type:'text_between',config:'number',value:''},
    };
    let list_cols = {
      id:{name:'id', label:'ID', width:'65px', fixed:'left', type:'hidden'},
      index:{name:'index', label:'期数', width:'60px'},
      dividend_ratio:{name:'dividend_ratio', label:'分红比例', width:'80px'},
      not_dividend_action:{name:'not_dividend_action', label:'未分红的处理', type:'radio', config:not_dividend_action_map, width:'100px'},
      can_dividend:{name:'can_dividend', label:'是否可分红', type:'radio', config:can_dividend_map, width:'100px'},

      pay_money_all:{name:'pay_money_all', label:'总业绩', width:'100px'},
      pay_money_need:{name:'pay_money_need', label:'业绩需求', width:'100px'},

      // dividend_num:{name:'dividend_num', label:'分红人数', width:'60px'},
      // dividend_money_unit:{name:'dividend_money_unit', label:'每人分红', width:'60px'},
      // dividend_level:{name:'dividend_level', label:'分红等级', type:'checkbox', config:[], width:'120px'},

      dividend_money_all:{name:'dividend_money_all', label:'总分红', width:'80px'},
      dividend_config:{name:'dividend_config', label:'分红配比', type:'json_param_checkbox', config:[], width:'220px'},
      dividend_result:{name:'dividend_result', type:'dividend_result', label:'分红结果', width:'260px'},
      dividend_money_surplus:{name:'dividend_money_surplus', label:'剩余', width:'80px'},

      note:{name:'note', label:'备注', type:'text_more', min_width:'120px'},
      status:{name:'status', label:'状态', type:'radio', config:status_map, width:'60px'},
      dividend_time: {name:'dividend_time', label:'分红时间', width:'145px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'145px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'145px'};
    }
    if (!this.admin_config('this_tenant.id')){
      list_cols['tenant_id'] = {name:'tenant_id', label:'租户', width:'65px', fixed:'right'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'150px', type:'action', fixed:'right'};

    let this_mouth = s94Mjs.date('Y-M-01 00:00:00');

    return {
      config: {
        api: '/admin/log_dividend/'+(is_del?'recycle':'list'),
        title: "分红列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          list.forEach(row=>{
            row.this_index = row.create_time>=this_mouth ? 1 : 0;
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
      sharing_receiver_map: {},

      change_form_config: change_form_config,
      admin_log:{
        show: false,
        list_cols: {
          create_time:{name:'create_time', label:'操作时间', width:'145px'},
          admin_nickname:{name:'admin_nickname', label:'管理员', width:'80px'},
          old:{name:'old', label:'修改前', type:'log_dividend', config:[]},
          new: {name:'new', label:'修改后', type:'log_dividend', config:[]},
        },
        list: []
      },
      level_map: {},
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('LogDividend/edit') && ~[0,row.tenant_id].indexOf(this.admin_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      this.iframe('edit?'+s94Mjs.param_encode(param))
    },
    to_money_list(row){
      this.iframe('./index_money?id='+row.id+'&this_index='+row.this_index);
    },
    show_admin_log(row){
      ajax({url:'/admin/log_dividend/admin_log?id='+row.id, loading:true}, ({data})=>{
        this.admin_log.show = true;
        this.admin_log.list = data;
      })
    },
    edit_dividend(row){
      this.confirm("是否确定执行分红操作? 如果选择手动分红，本期的自动分红将失效。自动分红：每月1号凌晨1点结算上一期的分红。", ()=>{
        ajax({url:'/admin/log_dividend/edit_dividend?id='+row.id, loading:true}, (res)=>{
          this.$refs.list_url.list();
        });
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    //其他初始化操作
    ajax({url:'/admin/log_dividend/config_map', cache:true}, ({data})=>{
      let dividend_config_config = {
        key_options: data.level_map,
        input: {type:"text_one", config:"number", inline:1, append:"%"},
      };
      this.config.list_cols.dividend_config.config = dividend_config_config;
      this.change_form_config.dividend_config.config = dividend_config_config;

      this.level_map = data.level_map;

      // this.config.list_cols.dividend_level.config = data.level_map;
      // this.change_form_config.dividend_level.config = data.level_map;
    })
  }
}
</script>
